<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('member.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-row :gutter="20">
                <el-col :span="4">
                  <el-form-item :label="i18n('member.avatar')" prop="avatar">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleAvatarSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('member.username')" prop="username">
                    <el-input v-model="dataForm.username" :placeholder="i18n('common.please_input') + i18n('member.username')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.chinese_nickname')" prop="chinese_nickname">
                    <el-input v-model="dataForm.chinese_nickname" :placeholder="i18n('common.please_input') + i18n('member.chinese_nickname')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.english_nickname')" prop="english_nickname">
                    <el-input v-model="dataForm.english_nickname" :placeholder="i18n('common.please_input') + i18n('member.english_nickname')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.chinese_mobile')" prop="chinese_mobile">
                    <el-input v-model="dataForm.chinese_mobile" :placeholder="i18n('common.please_input') + i18n('member.chinese_mobile')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.english_mobile')" prop="english_mobile">
                    <el-input v-model="dataForm.english_mobile" :placeholder="i18n('common.please_input') + i18n('member.english_mobile')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.email')" prop="email">
                    <el-input v-model="dataForm.email" :placeholder="i18n('common.please_input') + i18n('member.email')"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('member.setting.exchange_total')" prop="exchange_total">
                    <el-input-number v-model="dataForm.setting.exchange_total" :placeholder="i18n('common.please_input') + i18n('member.setting.exchange_total')" :min="1" :max="999"></el-input-number>
                  </el-form-item>

                  <el-form-item :label="i18n('member.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never" v-if="1 != dataForm.market_status">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('member.market.info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-form-item :label="i18n('member.market.first_level_rate')" prop="first_level_rate">
                <el-input-number v-model="dataForm.market.first_level_rate" :placeholder="i18n('common.please_input') + i18n('member.market.first_level_rate')" :min="0" :max="100"></el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('member.market.second_level_rate')" prop="second_level_rate">
                <el-input-number v-model="dataForm.market.second_level_rate" :placeholder="i18n('common.please_input') + i18n('member.market.second_level_rate')" :min="0" :max="100"></el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('member.market.market_status')" prop="market_status">
                <el-switch
                  v-model="dataForm.market_status"
                  active-value="1"
                  :active-text="i18n('common.yes')"
                  inactive-value="2"
                  :inactive-text="i18n('common.no')">
                </el-switch>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:member:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="member-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('member')

  const dataForm = reactive({
    id: 0,
    organization_id: '',
    role_id: 1,
    avatar: '',
    username: '',
    chinese_nickname: '',
    english_nickname: '',
    chinese_mobile: '',
    english_mobile: '',
    email: '',
    market_status: '2',
    status: '1',
    setting: {
      exchange_total: 1
    },
    contract: {},
    market: {}
  })

  const dataRule = reactive({
    username: [{
      required: true,
      message: i18n('member.rules.username.require'),
      trigger: 'blur',
    }],
    chinese_nickname: [{
      required: true,
      message: i18n('member.rules.chinese_nickname.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleAvatarSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/member/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.data && data.status === 200) {
            dataForm.organization_id = data.data.organization_id
            dataForm.role_id = data.data.role_id
            dataForm.avatar = data.data.avatar
            dataForm.username = data.data.username
            dataForm.chinese_nickname = data.data.chinese_nickname
            dataForm.english_nickname = data.data.english_nickname
            dataForm.chinese_mobile = data.data.chinese_mobile
            dataForm.english_mobile = data.data.english_mobile
            dataForm.email = data.data.email
            dataForm.market_status = data.data.market_status.value + ''
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/setting/data`),
              method: 'get',
              params: $http.adornParams({
                'member_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.setting.exchange_total = data.data.exchange_total
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/market/data`),
              method: 'get',
              params: $http.adornParams({
                'member_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.market.first_level_rate = data.data.first_level_rate
                dataForm.market.second_level_rate = data.data.second_level_rate
              }
            })
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
